<script setup>
import { ref, onMounted } from 'vue'
import {
  getProductListBySupplierIdService,
  delSupplierProductByIdService,
  getProductListBySupplierIdQueryinfService
} from '@/api/supplierManage'
import { Delete } from '@element-plus/icons-vue'
import EditAdd from '../components/EditAdd.vue'
import ProductAdd from '../components/ProductAdd.vue'
import { usePurchaserStore } from '@/stores'
const supplier = ref([])
const loading = ref(false)
const isQuery = ref(false)
const queryinf = ref('')
const dialog = ref()
const dialog2 = ref()
const purchaserStore = usePurchaserStore()

onMounted(() => {
  supplier.value = purchaserStore.supplier
  getProductList()
})

const getProductList = async () => {
  loading.value = true
  const res = await getProductListBySupplierIdService(
    purchaserStore.supplier.supplierId
  )
  supplier.value.products = res.data.data
  loading.value = false
}

const getlList = async () => {
  loading.value = true
  isQuery.value = true
  const res = await getProductListBySupplierIdQueryinfService(
    purchaserStore.supplier.supplierId,
    queryinf.value
  )
  supplier.value.products = res.data.data
  loading.value = false
}

const onDel = async (row) => {
  await delSupplierProductByIdService(
    purchaserStore.supplier.supplierId,
    row.productId
  )
  ElMessage.success('删除成功')
  getProductList()
}

const onAdd = () => {
  dialog2.value.open({
    id: supplier.value.supplierId,
    products: supplier.value.products
  })
}

const onEdit = () => {
  dialog.value.open(supplier.value)
}
const onSuccess = () => {
  getProductList()
}
</script>

<template>
  <page-container title="供应商详情">
    <template #extra>
      <ElInput v-model="queryinf" clearable style="width: 200px"></ElInput>
      <el-button @click="getlList">快速查询产品</el-button>
      <el-button @click="onAdd">添加供应物品</el-button>
      <el-button @click="onEdit">修改供应商信息</el-button>
    </template>

    <el-descriptions title="">
      <el-descriptions-item label="供应商名称">{{
        supplier.supplierName
      }}</el-descriptions-item>
      <el-descriptions-item label="联系人名称">{{
        supplier.contactName
      }}</el-descriptions-item>
      <el-descriptions-item label="联系电话">{{
        supplier.contactPhone
      }}</el-descriptions-item>
      <el-descriptions-item label="地址">{{
        supplier.address
      }}</el-descriptions-item>
    </el-descriptions>

    <el-table v-loading="loading" :data="supplier.products" style="width: 100%">
      <el-table-column type="index" label="序号" width="100"></el-table-column>
      <el-table-column prop="productName" label="产品名称"></el-table-column>
      <el-table-column
        prop="productDescription"
        label="产品描述"
      ></el-table-column>
      <el-table-column prop="categoryName" label="产品分类"></el-table-column>
      <el-table-column prop="supplyPrice" label="供应单价"></el-table-column>
      <el-table-column label="操作" width="200">
        <!-- row 就是 channelList 的一项， $index 下标 -->
        <template #default="{ row, $index }">
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDel(row, $index)"
          ></el-button>
        </template>
      </el-table-column>

      <template #empty>
        <el-empty
          description="该供应商暂不提供任何产品"
          v-if="isQuery === true"
        ></el-empty>
        <el-empty
          description="请输入关键字查询产品"
          v-if="isQuery === false"
        ></el-empty>
      </template>
    </el-table>

    <edit-add ref="dialog" @success="onSuccess"></edit-add>
    <product-add ref="dialog2" @success="onSuccess"></product-add>
  </page-container>
</template>

<style lang="scss" scoped></style>
